package com.rivulet.foreign.controller;

import com.rivulet.foreign.pojo.User;
import com.rivulet.foreign.pojo.UserInformation;
import com.rivulet.foreign.service.RootService;
import com.rivulet.foreign.service.ToolService;
import com.rivulet.foreign.service.UserService;
import com.rivulet.foreign.tools.Lock;
import com.rivulet.foreign.tools.Results;
import com.rivulet.foreign.tools.TimeTool;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.rivulet.foreign.tools.Identity.ROOT;

/**
 * {管理员界面跳转}
 *
 * @author xujiac
 * @create 2021/7/22
 * @since 1.0.0
 */
@RequestMapping(value = "/root")
@Controller
public class RootController {
    final
    RootService rootService;
    final
    ToolService authorizationForIdentity;
    final
    UserService userService;

    public RootController(RootService rootService, ToolService authorizationForIdentity, UserService userService) {
        this.rootService = rootService;
        this.authorizationForIdentity = authorizationForIdentity;
        this.userService = userService;
    }

    @RequestMapping(value = "/account/add")
    @ResponseBody
    public Results addUserAccount(@RequestBody User user, @CookieValue(value = "user_id") String user_id) {
        if (!authorizationForIdentity.reAuthorization(user_id, ROOT)) {
            return null;
        }
        if(user.getUser_id()==null||user.getUser_id().equals("")){
            return new Results(400,null);
        }
        Results<Object> results = rootService.addUser(user);
        return results;
    }

    @RequestMapping(value = "/account/setUserInformation")
    @ResponseBody
    public Results setUserInformation(@RequestParam("list_file") MultipartFile file,@CookieValue(value = "user_id") String user_id){
        if (!authorizationForIdentity.reAuthorization(user_id, ROOT)) {
            return null;
        }
        Results results = null;
        try {
            InputStream fileStream=file.getInputStream();
            HSSFWorkbook book=new HSSFWorkbook(fileStream);
            HSSFSheet sheet=book.getSheetAt(0);
            Row hear=sheet.getRow(0);
            List<UserInformation> list=new ArrayList<>();
            for(Row row:sheet){
                if(row.getRowNum()==0){
                    continue;
                }
                Integer sex;
                if(row.getCell(5).getStringCellValue().equals("男")){
                    sex=1;
                }
                else if(row.getCell(5).getStringCellValue().equals("女")){
                    sex=0;
                }else {
                    sex=-1;
                }
                for(Cell cell:row){
                    cell.setCellType(CellType.STRING);
                }
                if(row.getCell(0).getStringCellValue()==null||row.getCell(0).getStringCellValue().equals("")){
                    break;
                }
                UserInformation info=new UserInformation(
                        row.getCell(0).getStringCellValue(),
                        row.getCell(1).getStringCellValue(),
                        row.getCell(2).getStringCellValue(),
                        row.getCell(3).getStringCellValue(),
                        row.getCell(4).getStringCellValue(),
                        sex,
                        TimeTool.StringToDate(row.getCell(6).getStringCellValue(),TimeTool.GetTimeType(row.getCell(6).getStringCellValue())),
                        row.getCell(7).getStringCellValue(),
                        row.getCell(8).getStringCellValue(),
                        TimeTool.StringToDate(row.getCell(9).getStringCellValue(),TimeTool.GetTimeType(row.getCell(9).getStringCellValue())),
                        TimeTool.StringToDate(row.getCell(10).getStringCellValue(),TimeTool.GetTimeType(row.getCell(10).getStringCellValue())),
                        row.getCell(11).getStringCellValue(),
                        TimeTool.StringToDate(row.getCell(12).getStringCellValue(),TimeTool.GetTimeType(row.getCell(12).getStringCellValue())),
                        row.getCell(13).getStringCellValue(),
                        TimeTool.StringToDate(row.getCell(14).getStringCellValue(),TimeTool.GetTimeType(row.getCell(14).getStringCellValue())),
                        TimeTool.StringToDate(row.getCell(15).getStringCellValue(),TimeTool.GetTimeType(row.getCell(15).getStringCellValue())),
                        row.getCell(16).getStringCellValue(),
                        row.getCell(17).getStringCellValue(),
                        row.getCell(18).getStringCellValue(),
                        row.getCell(19).getStringCellValue(),
                        row.getCell(20).getStringCellValue(),
                        row.getCell(21).getStringCellValue(),
                        row.getCell(22).getStringCellValue()
                );
                list.add(info);
            }
            results=userService.addUser(list);
            fileStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            results.setCode(400);
            results.setMsg("文件格式出错");
        }
        return results;
    }

    @RequestMapping(value = "/account/delete")
    @ResponseBody
    public Results delUserAccounts(@RequestBody String []del_users, @CookieValue(value = "user_id") String user_id) {
        if (!authorizationForIdentity.reAuthorization(user_id, ROOT)) {
            return null;
        }
        Results<Object> results = rootService.delUser(del_users);
        return results;
    }

    @RequestMapping(value = "/account/check")
    @ResponseBody
    public Results checkUserAccounts(@CookieValue(value = "user_id") String user_id) {
        if (!authorizationForIdentity.reAuthorization(user_id, ROOT)) {
            return null;
        }
        Results<List> results = rootService.checkUsers(Lock.lockOff(user_id));
        return results;
    }

    @RequestMapping(value = "/account/reSetUser")
    @ResponseBody
    public Results reSetUser(@RequestBody User user,@CookieValue(value = "user_id")String user_id){
        if (!authorizationForIdentity.reAuthorization(user_id, ROOT)) {
            return null;
        }
        Results<Object> results = rootService.reSetUser(user);
        return results;
    }

    @RequestMapping(value = "account/searchUsers")
    @ResponseBody
    public Results searchAccount(@RequestBody User user,@CookieValue(value = "user_id")String user_id){
        if (!authorizationForIdentity.reAuthorization(user_id, ROOT)) {
            return null;
        }
        Results<Object> results = rootService.searchUser(user);
        return results;
    }
}
